function initTree(el){
    $(el).tree({
        onClick: function(node) {
            var title = node.text;
            var url = node.attributes['url'];
            var isIframe = node.attributes['isIframe'];
            addTabs(title, url, isIframe);
        }
    });
}
function addTabs(title, url, isIframe){
    var e = $TABS_CON.tabs('exists', title);
    if(e){//已经存在，就选中，并刷新
        $TABS_CON.tabs('select', title);
        var currTab = $TABS_CON.tabs('getSelected');
        if(isIframe){
            $TABS_CON.tabs('update',{
                tab:currTab,
                fit: true,
                options:{
                    content: '<iframe scrolling="no" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>'
                }
            });
        }else {
            $TABS_CON.tabs('update',{
                tab:currTab,
                href: url
            });
        }
    }else{
        // 没有被添加，就创建Tab
        if(isIframe){
            $TABS_CON.tabs('add',{
                title: title,
                fit: true,
                bodyCls: 'user-main',
                content: '<iframe scrolling="no" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>',
                closable:true,
                loadingMessage: '加载中'
            });
        }else {
            $TABS_CON.tabs('add',{
                title: title,
                href: url,
                fit: true,
                bodyCls: 'user-main',
                // content: '<iframe scrolling="no" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>',
                closable:true,
                loadingMessage: '加载中'
            });
        }
    }
}

//parenntId：需要加载子菜单的id  el：菜单容器 isMain：是否是一级菜单
function loadMenu(conId, menuCode){
    $.ajax({
        url: '/main/menu/data',
        data: {parent: encodeURI(menuCode)},
        success: function(data){
            console.log(data);
            secondData(data, conId);
        },
        error: function(){
            msg('加载菜单出错，请刷新页面重试');
        }
    });
}
//没用
function mainData(menuData, el, isMain){
    var rows = menuData['rows'];
    $(rows).each(function(i, o){
        var select = false;
        if(i == 0){
            select = true;
        }
        var a = $(el).accordion('add', {
            id: o['id'],
            title: o['name'],
            content: '',
            selected : select
        });
        $(a).accordion({
            onSelect: function(title, index){
                //加载菜单
                loadMenu1(a[0], false, title);
            }
        })
    });
}

function secondData(menuData, id){
    var rows = menuData['rows'];
    var data = handleMenuData(rows);
    $('#'+id).tree({
        data: data,
        onSelect: function (node) {
           var url = node.attributes['url'];
           var title = node.text;
           var isIframe = true;
           addTabs(title, url, isIframe);
        }

    })

}
function handleMenuData(rows){
    var data = [];
    $(rows).each(function(i, o){
        data.push(
            {'id': o['id'], 'text': o['name'], attributes: {'url': o['url'], 'code': o['code']} }
        )
    });
    return data;
}